﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ZLDC.Common.Estate;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web;

namespace ZLDC.Topo.BLL.Estate
{
    public class BuildBLL
    {
                                        public static string BuildList(int eid)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_listall_table(eid, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

                                                public static string BuildIdListByEid(int eid)
        {
            string idlist = "";

            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_listall_table(eid, db);

                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        idlist += dt.Rows[i]["id"].ToString();

                        if (i < dt.Rows.Count - 1)
                            idlist += ",";
                    }
                }
            }

            return idlist;
        }

                                                public static string BuildIdListByEname(string ename)
        {
            DataTable dt = null;
            int total = 0;           
            string useage = HttpContext.Current.Session["useage"].ToString();
            using (_Database db = new _Database("estate"))
            {
                if (useage != "")
                    dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_condition_table(1, 500, " where delflag = 0 and usage in("+useage+") and finalname like'" + ename + "%' ", ref total, db);
                else
                    dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_condition_table(1, 500, " where delflag = 0 and finalname like'" + ename + "%' ", ref total, db);
            }

            _Json json = new _Json(dt);

            if (dt != null && dt.Rows.Count > 0)
                return "{record:" + json.ToJson() + "}";
            else
                return "";
        }


                                                        public static string BuildIdListByEname(string ename, string bname)
        {
            DataTable dt = null;
            int total = 0;           
            string finalname = ename + bname;            string finalnamepy = _Utility.GetChineseSpell(finalname);
            string useage = HttpContext.Current.Session["useage"].ToString();
            string condition = "";
            if(useage!="")
                condition = "where delflag = 0 and usage in("+useage+") and (finalname like'" + finalname + "%' or finalnamepy like '" + finalnamepy + "%') ";
            else    
                condition = "where delflag = 0 and (finalname like'" + finalname + "%' or finalnamepy like '" + finalnamepy + "%') ";



            using (_Database db = new _Database("estate"))
            {

                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_condition_table(1, 500, condition, ref total, db);
            }

            _Json json = new _Json(dt);

            if (dt != null && dt.Rows.Count > 0)
                return "{record:" + json.ToJson() + "}";
            else
                return "";
        }

                                                        public static string BuildIdListByEnameAndBname(string ename, string bname)
        {
            DataTable dt = null;
            int total = 0;           
           
            string namepy = _Utility.GetChineseSpell(bname);
            string condition = "where delflag = 0  and ename='" + ename + "' and (finalname like '%" + bname + "%' or finalnamepy like '%" + namepy + "%') ";



            using (_Database db = new _Database("estate"))
            {

                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_condition_table(1, 500, condition, ref total, db);
            }

            _Json json = new _Json(dt);

            if (dt != null && dt.Rows.Count > 0)
                return "{record:" + json.ToJson() + "}";
            else
                return "";
        }
        public static string BuildIdListByEnameAndBname(string ename)
        {
            DataTable dt = null;
            int total = 0;           
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_condition_table(1, 500, " where delflag = 0 and ename ='" + ename + "' ", ref total, db);
            }

            _Json json = new _Json(dt);

            if (dt != null && dt.Rows.Count > 0)
                return "{record:" + json.ToJson() + "}";
            else
                return "";
        }

                                                public static string BuildList(int page, int pagesize, int eid)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_table(page, pagesize, eid, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                                public static string BuildList(int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_condition_table(page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                public static string BuildListLevel1(int page, int pagesize, int eid,string where,int hid)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_level_table1(page, pagesize, eid, ref total, db,where,hid);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }
                                                public static string BuildListLevel(int page, int pagesize, int eid)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_level_table(page, pagesize, eid, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                public static DataTable listdg(string eid)
        {
            DataTable dt = null;
            string sql = "select finalName, name ,id,eid from t_build where eid=" + eid + " and isValid=0";
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_dg(sql, db);
            }
            return dt;
        }

                                                        public static string BuildAutoList(int top, string condition)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_table(top, condition, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }
                                                        public static string BuildAutonewList(int top, string condition)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_newlist_table(top, condition, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }
                                                        public static string BuildAutoListString(int top, string condition)
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_table(top, condition, db);

                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sb.Append(dt.Rows[i]["finalname"].ToString());
                        sb.Append(" ");
                        sb.Append(dt.Rows[i]["othername"].ToString());
                        sb.Append(" ");
                        sb.Append(dt.Rows[i]["roadname"].ToString());
                        sb.Append("\n");
                    }
                }
            }

            return sb.ToString();
        }

                                                        public static string BuildAutoListString(int top, string condition, string condition2)
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_table2(top, condition, condition2, db);

                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sb.Append(dt.Rows[i]["finalname"].ToString());
                        sb.Append(" ");
                        sb.Append(dt.Rows[i]["othername"].ToString());
                        sb.Append(" ");
                        sb.Append(dt.Rows[i]["roadname"].ToString());
                        sb.Append("\n");
                    }
                }
            }

            return sb.ToString();
        }


                                                                public static string HousingMSAuto(int top, string condition, string condition2)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_list_table2(top, condition, condition2, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

                                                        public static int GetBuildByName(string buildName, ref int level)
        {
            int id = 0;
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.GetBuildByName(buildName, db);

                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        id = _Convert.ToInt(dt.Rows[i]["id"].ToString(), 0);
                        level = _Convert.ToInt(dt.Rows[i]["level"].ToString(), 0);
                    }
                }
            }

            return id;
        }

                                                        public static string EnameAutoList(int top, string condition)
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_Estate_list_table(top, condition, db);

                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sb.Append(dt.Rows[i]["name"].ToString());
                        sb.Append("\n");
                    }
                }
            }

            return sb.ToString();
        }

                                public static void LoadBuild(HtmlSelect ddlCity, int eid, string estateName)
        {
            using (_Database db = new _Database("estate"))
            {
                DataTable dt = ZLDC.DAL.Estate.BuildDAL.p_build_listall_table(eid, db,0);
                ddlCity.DataSource = dt;
                ddlCity.DataTextField = "finalName";
                ddlCity.DataValueField = "id";
                ddlCity.DataBind();

                ListItem item = new ListItem();
                item.Text = estateName;
                item.Value = "0";
                ddlCity.Items.Insert(0, item);
            }
        }

                                        public static string EstateSave(int id, string name, string otherName, string namePy, string otherNamePy, int parentId, string usage, int floorTotal, string finalName, int eid, string ename, string remark, int isValid, string registername, int registerid, string registerdept, int registerdeptid,string type)
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.BuildDAL.p_build_save(id, name, otherName, namePy, otherNamePy, parentId, usage, floorTotal, finalName, eid, ename, remark, 0, isValid, registername, registerid, registerdept, registerid,type, out outmsg, out newid, db);
            }

            return outmsg;
        }

                public static int EstateAndRoomNOSave(int id, string name, string otherName, string namePy, string otherNamePy, int parentId, string usage, int floorTotal, string finalName, int eid, string ename, string remark, int isValid, string registername, int registerid, string registerdept, int registerdeptid,string type)
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.BuildDAL.p_build_save(id, name, otherName, namePy, otherNamePy, parentId, usage, floorTotal, finalName, eid, ename, remark, 0, isValid, registername, registerid, registerdept, registerid, type, out outmsg, out newid, db);
            }

            return newid;
        }

                                        public static string BuildDetail(int id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.BuildDetail(id, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

        public static DataTable BuildDetail1(int id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.BuildDetail(id, db);
            }
            return dt;
        }

                                        public static string SelectNamesById(int id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.SelectNamesById(id, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

        public static string SelectNamesByBId(int id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.SelectNamesByBId(id, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

        public static string SelectBuildNameByBId(int id)
        {
            string buildName = string.Empty;
            DataTable dt = new DataTable(); ;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.BuildDetail(id, db);
            }

            if (dt.Rows.Count > 0)
                buildName = dt.Rows[0]["finalname"].ToString();
            return buildName;
        }

                                        public static string EstateDelete(int id)
        {
            string outmsg = "";

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.BuildDAL.p_build_delete(id, out outmsg, db);
            }

            return outmsg;
        }

                                                                                                public static int UpdateBName(string id, string oname, string type, string tablename, string commitName, string commitDept, string commitDeptID)
        {
            return ZLDC.DAL.Estate.BuildDAL.UpdateBName(id, oname, type, tablename, commitName, commitDept, commitDeptID);
        }

                public static string p_build_recycle_list_table(int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_build_recycle_list_table(page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";

        }

                public static string DeleteBuild(int id)
        {
            string outmsg = "删除失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.BuildDAL.DeleteBuild(id, db);
            }

            if (newid > 0)
                outmsg = "删除成功";

            return outmsg;
        }

                public static string DelBuild(int id)
        {
            string outmsg = "栋阁删除失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.BuildDAL.DelBuild(id, db);
            }

            if (newid > 0)
                outmsg = "栋阁删除成功";

            return outmsg;
        }
                public static string DelBuildAndIsValid(int id)
        {
            string outmsg = "您无权删除有效栋阁！";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.BuildDAL.DelBuildAndIsValid(id, db);
            }

            if (newid > 0)
                outmsg = "栋阁删除成功";

            return outmsg;
        }

                public static string SHBuild(string sql)
        {
            string outmsg = "栋阁审核失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.BuildDAL.SHBuild(sql, db);
            }

            if (newid > 0)
                outmsg = "栋阁审核失败";

            return outmsg;
        }
                public static string UpdateDelFlag(int id)
        {
            string outmsg = "恢复失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.BuildDAL.UpdateDelFlag(id, db);
            }

            if (newid > 0)
                outmsg = "恢复成功";

            return outmsg;
        }

                                                public static string BuildCheck(int id)
        {
            string outmsg = "栋阁审核失败";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.BuildDAL.BuildCheck(id, db);
                if (rows > 0)
                    outmsg = "栋阁审核成功";
            }

            return outmsg;
        }
        public static string BuildCheck(int id, int isvalid, string Approval_views)
        {
            string outmsg = "栋阁审核失败";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.BuildDAL.BuildCheck(id, isvalid, Approval_views, db);
                if (rows > 0)
                    outmsg = "栋阁审核成功";
            }

            return outmsg;
        }

                                                        public static string p_v_build_list_table(int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.BuildDAL.p_v_build_list_table(page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt, 1);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                        public static int SelectCountByParentId(int id)
        {
            int count = 0;
            using (_Database db = new _Database("estate"))
            {
                count = ZLDC.DAL.Estate.BuildDAL.SelectCountByParentId(id, db);
            }
            return count;
        }

                public static string UpdateParentIdById(int id, int parentId)
        {
            string outmsg = "";
            using (_Database db = new _Database("estate"))
            {
                outmsg = ZLDC.DAL.Estate.BuildDAL.UpdateParentIdById(id, parentId, db);
            }
            return outmsg;
        }
                                                        public static string RemoveRoomNoByBid(int id, int bid)
        {
            string outmsg = "";
            using (_Database db = new _Database("estate"))
            {
                outmsg = ZLDC.DAL.Estate.BuildDAL.RemoveRoomNoByBid(id, bid, db);
            }
            return outmsg;
        }

        public static string ExistsRoomNo(int id, int bid)
        {
            using (_Database db = new _Database("estate"))
            {
                return ZLDC.DAL.Estate.BuildDAL.ExistsRoomNo(id, bid, db);
            }
        }
    }
}
